package com.briup.es.service.impl;

import com.briup.es.config.auth.UserDetails;
import com.briup.es.domain.bean.Address;
import com.briup.es.domain.vo.AddressVO;
import com.briup.es.mapper.AddressMapper;
import com.briup.es.mapper.UserMapper;
import com.briup.es.service.AddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public List<AddressVO> queryAllByUserId() {
        // 获取当前登录用户的id
        Integer userId = UserDetails.userId();
        // 查询当前登录用户的所有收货地址
        return addressMapper.queryAllByUserId(userId);
    }

    @Override
    public void addAddress(Address address) {
        // 获取当前登录用户的id
        Integer userId = UserDetails.userId();
        // 将用户id填入地址实体对象中
        address.setUserId(userId);
        addressMapper.addAddress(address);
    }

    @Override
    public void updateAddress(Address address) {
        addressMapper.updateMapper(address);
    }

    @Override
    public void deleteAddress(Integer id) {
        addressMapper.deleteAddress(id);
    }

    public void updateDefault(Integer addressId) {
        Integer userId = UserDetails.userId();
        userMapper.updateDefault(userId, addressId);
    }
}
